/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://iv-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - Core/mysql.nut                                     *
*                                                                               *
*  The MySQL class of the gamemode comes here.                                  *
*  @author: Matthias <matthias@sa-mp.nl>                                        *
*                                                                               *
********************************************************************************/

/*
 * Class: MySQL
 * 
 * This class contains all database related functions, such as connecting to the MySQL
 * server, querying it, etc.
 */
class MySQL
{
    // These variables will hold the MySQL details.
    sServer = "localhost";
    sUser = "root";
    sPassword = "lvpdb";
    sDatabase = "lvp";
   
    /**
     * This function connects to the MySQL with the specified details.
     *
     * @return void
     */
    function connect()
    {
        // Create the connection with the server.
        connectionHandle = mysql_connect (sServer, sUser, sPassword, sDatabase);
    }
    
    /**
     * This function checks whether the gameserver is connected to the MySQL database.
     *
     * @return boolean true if the connect attempt was succesful, false otherwise.
     */
    function isConnected()
    {
        if (!connectionHandle || !mysql_ping (connectionHandle)) return false;
        return true;
    }
    
    /**
     * This function disconnects the gameserver from the MySQL server.
     *
     * @return void
     */
    function disconnect()
    {
        // Make sure we're not connected already.
        if (this.isConnected())
        {
            mysql_close (connectionHandle);
            
            connectionHandle = false;
        }
    }
    
    /**
     * This function shows an error in the console.
     *
     * @return void
     */
    function error()
    {
        // We make sure an error actually occured.
        if (mysql_errno (connectionHandle) > 0)
        {
            // And log it.
            log ("[MySQL] Error: " + mysql_errno (connectionHandle ).tostring () + " " + mysql_error (connectionHandle));
        }
    }
    
    /**
     * This function returns a properly escaped string.
     *
     * @param string The string to escape.
     * @return false if there is no connection. Otherwise, it returns the escaped string.
     */
    function escapeString( string )
    {
        if (this.isConnected ())
        {
            return mysql_escape_string (connectionHandle, string);
        }
        return false;
    }

    /**
     * This function reconnects if the connection is dead.
     *
     * @return true if it had to reconnect, false otherwise.
     */
    function checkConnection()
    {
        if (!this.isConnected())
        {
            this.connect ();
            return true;
        }        
        return false;
    }
    
    /**
     * This function executes a query on the MySQL server.
     *
     * @param sQuery The query to execute on the server.
     * @return true if the query was executed correctly, false otherwise.
     */
    function query( sQuery )
    {
        if(this.isConnected ())
        {
            local Result = mysql_query (connectionHandle, sQuery);
            if (Result)
            {
                return Result;
            }
            else 
            {
                return false;
            }
        }
        return false;
    }
    
    /**
     * This function fetches an assoc array from a certain result.
     *
     * @param Result The result set to get the array from.
     * @return false if the server is not connected, otherwise it returns the array.
     */
    function fetch_assoc( Result )
    {
        if(this.isConnected ())
        {
            return mysql_fetch_assoc (Result);
        }
        return false;
    }
    
    /**
     * This funcion returns the last inserted id from a query.
     *
     * @return id The id that was last inserted
     */
    function last_inserted()
    {
        if(this.isConnected ())
        {
            return mysql_insert_id (connectionHandle);
        }
        return false;
    }
}